package com.sinopec.handdevice.presenter;

import android.text.TextUtils;

import com.orhanobut.hawk.Hawk;
import com.sinopec.basemodule.AppConstants;
import com.sinopec.basemodule.constants.RouterConstants;
import com.sinopec.basemodule.http.CommonObserver;
import com.sinopec.basemodule.http.ExceptionEngine;
import com.sinopec.basemodule.http.api.HttpResult;
import com.sinopec.basemodule.model.app_model.WelcomeModel;
import com.sinopec.basemodule.model.login_model.ConfigInfo;
import com.sinopec.basemodule.router.ReflectUtils;
import com.sinopec.basemodule.utils.ProvinceCityUtils;
import com.sinopec.handdevice.MyApp;
import com.sinopec.handdevice.api.AppApi;
import com.sinopec.handdevice.contract.WelcomeContract;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

public class WelcomePresenter extends WelcomeContract.Presenter {
    public WelcomePresenter(WelcomeContract.View view) {
        super(view);
    }

    @Override
    public void requestConfig() {
        Observable<HttpResult<ConfigInfo>> httpResultObservable = AppApi.getInstance().requestConfig();
        toSubscribe(httpResultObservable, new CommonObserver<ConfigInfo>(mView.getContext(), false) {
            @Override
            public void onError(ExceptionEngine.ResponseThrowable t) {
                //requestSplashImg();
                getStarTheFigure();
            }

            @Override
            public void onNext(ConfigInfo configInfo) {
                ConfigInfo config = Hawk.get(AppConstants.HAWK_CONFIG);
                if (config != null && Integer.parseInt(TextUtils.isEmpty(config.getAppJsonVersion()) ? "0" : config.getAppJsonVersion()) < Integer.parseInt(TextUtils.isEmpty(configInfo.getAppJsonVersion()) ? "0" : configInfo.getAppJsonVersion())) {
                    configInfo.setNeedUpdate(true);
                    if (!TextUtils.isEmpty(configInfo.getAppJsonSrc())) {
                        processDownLoad(configInfo.getAppJsonSrc());
                    }
                } else if (config != null) {
                    config.setNeedUpdate(false);
                }
                //configInfo.setNeedUpdate(true);
                //processDownLoad("https://images.roshare.com/images/real/app/qiya/start/muqinjie2019.jpg");
                Hawk.put(AppConstants.HAWK_CONFIG, configInfo);
                getStarTheFigure();
            }
        });
    }

    /**
     * @author lwj
     * @Description: 下载文件
     * @date 2019/6/19 13:23
     */
    public void processDownLoad(String appJsonSrc) {
        Observable<ResponseBody> observable = AppApi.getInstance().downloadFile(appJsonSrc);
        observable
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<ResponseBody>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        String tempFilePath = ProvinceCityUtils.saveFile(responseBody);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 登录或主界面
     */
    public void gotoLoginOrMain() {
        if (MyApp.getInstance().getUserManager().getUser() != null
                && MyApp.getInstance().getToken() != null) {
            ReflectUtils.navigationToHome(mView.getContext(), 0);
        } else {
            ReflectUtils.startActivityWithName(mView.getContext(), RouterConstants.PATH_LOGIN);
        }
        mView.getContext().finish();
    }

    /**
     * @author lwj
     * @Description: 查询app启动图
     * @date 2019/3/6 11:00
     */
    @Override
    public void getStarTheFigure() {
        Observable<HttpResult<WelcomeModel>> httpResultObservable = AppApi.getInstance().getStarTheFigure();
        toSubscribe(httpResultObservable, new CommonObserver<WelcomeModel>(mView.getContext(), false) {
            @Override
            public void onNext(WelcomeModel model) {
                mView.refreshUI(model);
            }

            @Override
            public void onError(ExceptionEngine.ResponseThrowable t) {
                mView.requestFail();
            }
        });
    }

    /**
     * 模拟启动页
     */
    private WelcomeModel getStarTheFigureData() {
        WelcomeModel welcomeModel = new WelcomeModel();
        welcomeModel.setFigureShowTime("6");//app启动图展示时长（秒）
        welcomeModel.setStarTheFigure("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=527508931,873576955&fm=26&gp=0.jpg");//app启动图
        welcomeModel.setFigureSkipLinks("");//app启动图跳转链接

        return welcomeModel;
    }
}
